# -*- coding: utf-8 -*-

import wmi
import os
import platform




class windows_system_lib(object):


    def __init__(self):
        pass


    def get_system(self):
        return platform.system()


    def get_system_sn(self):
        wmi_obj = wmi.WMI()
        computer_info = wmi_obj.Win32_ComputerSystem()[0]
        system_info = wmi_obj.Win32_OperatingSystem()[0]
        return system_info.SerialNumber


    def get_hostname(self):
        sys = os.name

        if sys == 'nt':
            hostname = os.getenv('computername')
            return hostname

        if sys == 'posix':
            host = os.popen('echo $HOSTNAME')
            try:
                hostname = host.read()
                return hostname
            finally:
                host.close()

        return None


    def memory_info(self):
        c = wmi.WMI()

        data = []
        for mem in c.Win32_PhysicalMemory():
            d1 = {}
            d1['UUID'] = mem.qualifiers['UUID'][1:-1]
            d1['BankLabel'] = mem.BankLabel
            d1['SerialNumber'] = mem.SerialNumber.strip()
            d1['Capacity'] = mem.Capacity
            data.append(d1)
        return data


def cpu_info():
    tmpdict = {}

    tmpdict["CpuCores"] = 0
    for cpu in c.Win32_Processor():
        tmpdict["cpuid"] = cpu.ProcessorId.strip()
        tmpdict["CpuType"] = cpu.Name
        tmpdict['systemName'] = cpu.SystemName
        try:
            tmpdict["CpuCores"] = cpu.NumberOfCores
        except:
            tmpdict["CpuCores"] += 1
        tmpdict["CpuClock"] = cpu.MaxClockSpeed
        tmpdict['DataWidth'] = cpu.DataWidth
    print (tmpdict)
    return  tmpdict


# 主板
def mainboard_info():
    data = []
    # print len(c.Win32_BaseBoard()):
    for board_id in c.Win32_BaseBoard():
        d1 = {}
        d1['UUID'] = board_id.qualifiers['UUID'][1:-1]  # 主板UUID,有的主板这部分信息取到为空值，ffffff-ffffff这样的
        d1['SerialNumber'] = board_id.SerialNumber  # 主板序列号
        d1['Manufacturer'] = board_id.Manufacturer  # 主板生产品牌厂家
        d1['Product'] = board_id.Product  # 主板型号
        data.append(d1)

    print(data)
    return data


# 获取 bios 信息
def bios_info():
    data = []
    for bios_id in c.Win32_BIOS():
        d1 = {}
        d1['BiosCharacteristics'] = bios_id.BiosCharacteristics  # BIOS特征码
        d1['version'] = bios_id.Version  # BIOS版本
        d1['Manufacturer'] = bios_id.Manufacturer.strip()  # BIOS固件生产厂家
        d1['ReleaseDate'] = bios_id.ReleaseDate  # BIOS释放日期
        d1['SMBIOSBIOSVersion'] = bios_id.SMBIOSBIOSVersion  # 系统管理规范版本
        d1['SerialNumber'] = bios_id.SerialNumber
        data.append(d1)

    print(data)
    return data


# 硬盘
def disk_info():
    disks = []
    for disk in c.Win32_DiskDrive():
        # print disk.__dict__
        tmpmsg = {}
        tmpmsg['SerialNumber'] = disk.SerialNumber.strip()
        tmpmsg['DeviceID'] = disk.DeviceID
        tmpmsg['Caption'] = disk.Caption
        tmpmsg['Size'] = disk.Size
        tmpmsg['UUID'] = disk.qualifiers['UUID'][1:-1]
        disks.append(tmpmsg)
    for d in disks:
        print (d)
    return disks




# 电池信息，只有笔记本才会有电池选项
def battery_info():
    isBatterys = False
    for b in c.Win32_Battery():
        isBatterys = True
    return isBatterys


def get_server_info():
    wmi_obj = wmi.WMI()
    computer_info = wmi_obj.Win32_ComputerSystem()[0]
    system_info = wmi_obj.Win32_OperatingSystem()[0]

    data = {}
    data['manufactory'] = computer_info.Manufacturer
    data['model'] = computer_info.Model
    data['wake_up_type'] = computer_info.WakeUpType
    data['sn'] = system_info.SerialNumber

    return data